import { _decorator, Component, director, Node, tween, v2, v3 } from 'cc';
import { AdComponent } from '../AdComponent';
import { Cubes } from '../../Cube/Cubes';
import { MainGameUI } from '../MainGameUI';
import DatasSpace from '../Tool/DatasSpace';
import *as cc from "cc";
import Util from '../Util';
import { EVENT_TYPE, MISSION_TYPE } from '../../Cube/Levels';
import EventMgr from '../Tool/EventMgr';
import { AudioMgr, ENUM_CLIP } from '../Tool/AudioMgr';
import SkdComm from '../SkdComm';
import GlobeData from '../GlobeData';
const { ccclass, property } = _decorator;

@ccclass('GamePause')
export class GamePause extends Component {

  @property(Node)
  viewNode: Node = null;

  @property(Node)
  rewPoint: Node = null;

  @property(cc.Label)
  rewBtnLbl: cc.Label = null;

  @property(cc.Toggle) toggle_vibrate: cc.Toggle = null;
  @property(cc.Toggle) toggle_music: cc.Toggle = null;

  @property(cc.Node)
  shareBtn:cc.Node=null;  
  @property(cc.Label)
  timeLabel:cc.Label[]=[];

  
  private pointTween = null;
  private rewPor = 1;
  
  
  btnExit() {
    this.btnClose();
    //打开是否确认退出
    Util.Instance.GloberLoadPrefab('IsExitMainGame', 2, (res) => {
      let newNode = cc.instantiate(res);
      cc.find('UI').getChildByName('PopView').addChild(newNode);
    }, () => {

    });
  }

  initSetting() {
    this.toggle_vibrate.isChecked = false;
    this.toggle_music.isChecked = DatasSpace.Instance.GameSound ? true : false;
  }

  btnMusic() {
    DatasSpace.Instance.GameSound = this.toggle_music.isChecked ? 1 : 0;
    console.log(DatasSpace.Instance.GameSound)
  }
  btnReplay() {
    //Cubes.inst.btnStart();
    this.btnCloseSetting();
    Util.Instance.GloberLoadPrefab('AgainGame', 2, (res) => {
      let newNode = cc.instantiate(res);
      cc.find('UI').getChildByName('PopView').addChild(newNode);
    }, () => {

    });
  }

  btnClose() {

    this.btnCloseSetting();
  }

  start() {
    DatasSpace.Instance.gamePause = true;
    // AdComponent.Instance.show5X5CustomAd();
    AdComponent.Instance.showInterstitialFullShow();
    AdComponent.Instance.showBannerAd();
    // tween(this.viewNode).to(0.3, { position:v3(this.viewNode.getPosition().x,-50,0), scale:v3(1.2,0.8,0)}, { easing: 'smooth' }).to(0.3, { position: v3(0,0,0), scale:v3(1,1,0) }, { easing: 'backInOut' }).to(0.3, { scale:v3(1.1,0.9,0)}, { easing: 'backInOut' }).to(0.1, { scale:v3(1,1,0) }, { easing: 'backInOut' }).call(() => {
    // }).start();
    if (this.rewPoint) this.startSelectRew();

    if (this.toggle_music) {
      this.initSetting();
    }
    if(this.timeLabel.length>0){
      for(let i=0;i<this.timeLabel.length;i++){
        this.timeLabel[i].string=GlobeData.Instance.freeCount+"/"+3;
      }
      if(GlobeData.Instance.freeCount>0){
        this.timeLabel[0].node.parent.active=false;
        this.timeLabel[1].node.parent.active=false;
        this.timeLabel[2].node.parent.active=true;
      }else{
        this.timeLabel[0].node.parent.active=true;
        this.timeLabel[1].node.parent.active=true;
        this.timeLabel[2].node.parent.active=false;
      }
    }
    
  }

  protected onDestroy(): void {
    DatasSpace.Instance.gamePause = false;
    AdComponent.Instance.hideBannerAd();
  }

  openLose(num): void {
    if(this.shareBtn){
      if(num==0){
        this.shareBtn.active=true;
      }else{
        this.shareBtn.active=false;
      }
    }
    
  }

  btnCloseSetting() {
    AudioMgr.Instance.play(ENUM_CLIP.Click);

    // AdComponent.Instance.instance.playAudio(2);
    // AdComponent.Instance.hide5X5customAd();
    tween(this.viewNode).to(0.5, { position: v3(0, this.viewNode.getPosition().y + 1600, 0) }, { easing: 'backInOut' }).call(() => {
      this.node.removeFromParent();
      this.node.destroy();

    }).start();
  }

  /**广告复活 */
  btnReliceByAd() {
    AdComponent.Instance.loadVideoAd(0, (res) => {
      if (res) {
        Cubes.inst.reLiveGame();
        this.btnCloseSetting();
      }
    });
    // Cubes.inst.btnStart();
  }

  /**分享复活 */
  btnReliveByShare() {
    SkdComm.Instance.shareTo(() => {
      Cubes.inst.reLiveGame();
      this.btnCloseSetting();
    })
  }


  /**广告重新开始 */
  btnAgainByAd() {
    AdComponent.Instance.loadVideoAd(0, (res) => {
      if (res) {
        cc.director.loadScene("MainGame");
      }
    });
    // Cubes.inst.btnStart();
  }

  /**分享重新开始*/
  btnAgainByShare() {
    SkdComm.Instance.shareTo(() => {
      cc.director.loadScene("MainGame");
    })
  }
  
  btnShareOnly(){
    SkdComm.Instance.shareTo(() => {
     
    })
  }
  btnAgain(){
    cc.director.loadScene("MainGame");
  }

  /**下一关 */
  btnNextLevel() {
    Cubes.inst.btnStart();
    DatasSpace.Instance.starNum += 100;
    DatasSpace.Instance.setStarNum();
    EventMgr.emit(EVENT_TYPE.EVENT_UPD_GAMEINFO)
    this.btnCloseSetting();

  }

  /**胜利界面双倍领取 */
  btnGetDoubleReward() {

    this.pointTween && this.pointTween.stop();

    AdComponent.Instance.loadVideoAd
    AdComponent.Instance.loadVideoAd(0, (res) => {
      if (res) {
        Cubes.inst.btnStart();
        DatasSpace.Instance.starNum += 100 * this.rewPor;
        DatasSpace.Instance.setStarNum();
        EventMgr.emit(EVENT_TYPE.EVENT_UPD_GAMEINFO)
        this.btnCloseSetting();

      }
    });
  }

  /**分享好友领取宝箱 */
  btnShareToFriend() {

  }

  private addTimeNum: number = 300;

  /**
   * 分享加时
   * time 2024 5 29 12:29
   */
  btnAddTimeByShare() {
    MainGameUI.inst.timer = this.addTimeNum;
    MainGameUI.inst.startCountDown();
    this.btnCloseSetting();
    cc.game.emit('showWord');
  }

  /**
   * @fn 金币加时
   * @time 2024 5 29 12:30
   */
  btnAddTimeByCostGold() {
    let addTimePrice: number = 100;
    if (addTimePrice > DatasSpace.Instance.goldNum) {
      console.log('金币不足');
    } else {
      DatasSpace.Instance.addMission_Target(MISSION_TYPE.USE_PROP_SHIZHONG, 1)

      DatasSpace.Instance.goldNum -= addTimePrice;
      DatasSpace.Instance.setGoldNum();
      MainGameUI.inst.timer = this.addTimeNum;
      MainGameUI.inst.startCountDown();
      this.btnCloseSetting();
      cc.game.emit('showWord');
    }

  }

  /**
   *@fn 视频广告加时
   *@time 2024 5 29 12:30
   * 
   */
  btnAddTimeByVideoAd() {

    AdComponent.Instance.loadVideoAd(0, (res) => {
      if (res) {
        MainGameUI.inst.timer = this.addTimeNum;
        MainGameUI.inst.startCountDown();
        this.btnCloseSetting();
        cc.game.emit('showWord');
      }
    });

  }

  /**
   * @fn 关闭时间到页面
   * @time 2024 5 29 12:41
   */
  btnCloseLoseByTimer() {
    //    this.btnExit();
    cc.director.loadScene('Home');

  }

  /**
   * @fn 退出游戏
   * @time 2024 5 29
   * 
   */
  btnQuitGame() {
    this.btnCloseSetting();
    director.loadScene('Home');
  }

  btnShare(){
    Util.Instance.GloberLoadPrefab('PropShare', 1, (res) => {
      let newNode = cc.instantiate(res);
      this.node.addChild(newNode);
    }, () => {

    });
  }


  startSelectRew() {
    let width = this.rewPoint.parent.getComponent(cc.UITransform).width;
    this.pointTween = tween(this.rewPoint)
      .to(2, { position: v3((width / 2) - 5, this.rewPoint.getPosition().y) }, { easing: 'sineOut' })
      .to(2, { position: v3(-((width / 2) - 5), this.rewPoint.getPosition().y) }, { easing: 'sineOut' })
      .union()
      .repeatForever()
      .start();
  }


  update(deltaTime: number) {
    if (this.rewPoint) {
      let pos = this.rewPoint.getPosition();
      let rewRange = [[-227, -175], [-175, -125], [-125, -75], [-75, 75], [75, 125], [125, 175], [175, 227]]
      for (let i = 0; i < rewRange.length; i++) {
        let range = rewRange[i];
        if (pos.x >= range[0] && pos.x < range[1]) {
          if (i == 0 || i == 6) {
            this.rewPor = 2;
          } else if (i == 1 || i == 5) {
            this.rewPor = 3;
          } else if (i == 2 || i == 4) {
            this.rewPor = 4;
          } else if (i == 3) {
            this.rewPor = 5;
          }
        }
      }
      this.rewBtnLbl.string = `领取${this.rewPor}倍`
    }
  }






}


